<?php
$str=curl('https://www.readnovel.com/');
//echo $str;
$preg='#热门推荐.*本周强推#isU';
preg_match($preg,$str,$arr);
//print_r($arr);die;
$preg_all='#<ul><li data-rid=".*"><div class=".*"><a href="(.*)" data-eid=".*" data-bid=".*" target="_blank"><img src=".*" alt=".*"></a></div><div class=".*"><h4><a href=".*" data-eid=".*" data-bid=".*" target="_blank" title=".*">(.*)</a></h4><p>(.*)</p><div class=".*"><i>(.*)</i><a class=".*" data-eid=".*" target="_blank"><img src=".*">(.*)</a></div></div></li>.*<li data-rid=".*"><div class=".*"><a href="(.*)" data-eid=".*" data-bid=".*" target="_blank"><img src=".*" alt=".*"></a></div><div class=".*"><h4><a href=".*" data-eid=".*" data-bid=".*" target="_blank" title=".*">(.*)</a></h4><p>(.*)</p><div class=".*"><i>(.*)</i><a class=".*" data-eid=".*" target="_blank"><img src=".*">(.*)</a></div></div></li>.*</ul><#isU';
preg_match_all($preg_all,$arr[0],$gg);
//print_r($gg);die;
foreach($gg[1] as $key=>$value)
{
    $arr_gg['novel_url']=$gg[1][$key];
    $arr_gg['novel_title']=$gg[2][$key];
    $arr_gg['novel_content']=$gg[3][$key];
    $arr_gg['novel_type']=$gg[4][$key];
    $arr_gg['novel_author']=$gg[5][$key];
    foreach($gg[6] as $key=>$value)
    {
        $arr_gg['novel_url']=$gg[6][$key];
        $arr_gg['novel_title']=$gg[7][$key];
        $arr_gg['novel_content']=$gg[8][$key];
        $arr_gg['novel_type']=$gg[9][$key];
        $arr_gg['novel_author']=$gg[10][$key];
        $dbh=new PDO('mysql:host=localhost;dbname=gaofei','root','root');
        $sql="insert into novel(novel_url,novel_title,novel_content,novel_type,novel_author) VALUES('{$arr_gg['novel_url']}','{$arr_gg['novel_title']}','{$arr_gg['novel_content']}','{$arr_gg['novel_type']}','{$arr_gg['novel_author']}')";
        $dbh->exec($sql);
    }

    $dbh=new PDO('mysql:host=localhost;dbname=gaofei','root','root');
    $sql="insert into novel(novel_url,novel_title,novel_content,novel_type,novel_author) VALUES('{$arr_gg['novel_url']}','{$arr_gg['novel_title']}','{$arr_gg['novel_content']}','{$arr_gg['novel_type']}','{$arr_gg['novel_author']}')";
    $dbh->exec($sql);


}
function curl($url)
{
    $ch=curl_init();//实例化
    curl_setopt($ch,CURLOPT_URL,$url);//设置地址
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    $str=curl_exec($ch);
    return $str;
}
